using GLib;
//using Gee;
using Rob;

namespace Rob
{
	namespace Rules
	{
		public class Time: Rob.Rules.Rule
		{
			public int starth{get;set;};
			public int startmin{get;set;};
			public int endh{get;set;};
			public int endmin{get;set;};
			public bool[] days{get,set};
			private uint id;
			construct
			{
				_days = new bool[ 7 ];
				for(int i = 0; i <7; i++)
					_days[ i ] = false;
				starttime = 0;
				endtime = 24*60;
				_title = "";
				_name = "";
				_interval = 60;
				_type = "time";
			}
			public bool get_day( GLib.DateWeekday i )
			{
				return this._days[(int)i];	
			}
			public void set_day( GLib.DateWeekday i, bool val)
			{
				this._days[(int)i]= val;	
			}
			
			~Time()
			{
				stdout.printf("Dtor\n");
			}	
		
			/*public int starttime
			{
				get{return _starttime;}
				set{ _starttime = (value > 0 && value < 24*60) ? (value < _endtime ) ? value:_starttime : 0; }
			}	
			public int endtime
			{
				get{return _endtime;}
				set{ _endtime = (value > 0 && value < 24*60) ? (value > _starttime ) ? value: _endtime : 0; }
			}	
			public int interval
			{
				get{ return _interval;}
				set{ _interval = value; }
			}
			/*public string title
			{
				get{return _title;}
				set{_title = value;}
			}
			public string name
			{
				get{return _name;}
				set{_name = value;}
			}*/	
			public override bool check()
			{
				GLib.TimeVal tv= GLib.TimeVal();
				tv.get_current_time( );
				GLib.Date date = GLib.Date();
				date.set_time_val(tv);
				GLib.debug("Day of the Week: %i\n", date.get_day());

				if( _days[ date.get_day() ] == true )
				{
					GLib.Time t ;
					date.to_time( out t );
					int mins = t.hour * 60 + t.minute;
					if( mins < this._endtime && this._starttime < mins )
						return true;
				}
				return false;
			}
			public /*override*/ bool load(GLib.KeyFile kf, string name)
			{
				try
				{
					base.load( kf, name );
					this._name = name;
					_days[ (int) GLib.DateWeekday.MONDAY ] = kf.get_boolean(name, "monday");
					_days[ (int) GLib.DateWeekday.TUESDAY ] = kf.get_boolean(name, "tuesday");
					_days[ (int) GLib.DateWeekday.WEDNESDAY ] = kf.get_boolean(name, "wednesday");
					_days[ (int) GLib.DateWeekday.THURSDAY ] = kf.get_boolean(name, "thursday");
					_days[ (int) GLib.DateWeekday.FRIDAY ] = kf.get_boolean(name, "friday");
					_days[ (int) GLib.DateWeekday.SATURDAY ] = kf.get_boolean(name, "saturday");
					_days[ (int) GLib.DateWeekday.SUNDAY ] = kf.get_boolean(name, "sunday");
					this.interval = kf.get_integer(name, "interval" );
					this.starttime = kf.get_integer(name, "starttime" );
					this.endtime = kf.get_integer( name, "endtime" );
				}

				catch( GLib.KeyFileError kfe)
				{
					return false;
				}
				return true;
			}
			public /*override*/ bool save(ref GLib.KeyFile kf)
			{
				base.save(ref kf);
				kf.set_boolean( _name, "monday", _days[ (int) GLib.DateWeekday.MONDAY ]);
				kf.set_boolean( _name, "tuesday", _days[ (int) GLib.DateWeekday.TUESDAY ]);
				kf.set_boolean( _name, "wednesday", _days[ (int) GLib.DateWeekday.WEDNESDAY ]);
				kf.set_boolean( _name, "thursday", _days[ (int) GLib.DateWeekday.THURSDAY ]);
				kf.set_boolean( _name, "friday", _days[ (int) GLib.DateWeekday.FRIDAY ]);
				kf.set_boolean( _name, "saturday", _days[ (int) GLib.DateWeekday.SATURDAY ]);
				kf.set_boolean( _name, "sunday", _days[ (int) GLib.DateWeekday.SUNDAY ]);
				kf.set_integer( _name, "interval", _interval );
				kf.set_integer( _name, "starttime", _starttime );
				kf.set_integer( _name, "endtime", _endtime );
				return true;	
			}
			public /*override*/ void run()
			{
				//GLib.TimeoutSource ts = add_timeout (Rob.maincontext, _interval, Rob.Rules.Time.time_cb , weak this);
				this.id = Timeout.add_seconds(this._interval, (GLib.SourceFunc)this.time_cb );
			}
			public /*override*/ void stop()
			{
				GLib.Source source = Rob.maincontext.current_source();
				source.remove(this.id);	
			}
			public static bool time_cb( void * data )
			{
				Rob.Rules.Time t = (Rob.Rules.Time) data;
				t.fulfilled = t.check();
				return true;
			}
		}
		public GLib.Type get_type_of_time()
		{
			return typeof(Rob.Rules.Time);
		}
	}
}
